<template>
  <el-scrollbar always :native="deviceType === 'h5'">
<!--    <VFormRender v-if="pageInfo.formData && pageInfo.scheme" v-bind="pageInfo" />-->
    <v-form-render ref="formRenderRef"  :scheme="scheme" :form-data="formData"></v-form-render>
  </el-scrollbar>
</template>
<script lang="ts" setup>
import VFormRender from '@/components/form/designer/VFormRender.vue';
import { ElScrollbar } from "element-plus";
import { getDeviceType } from "@/utils/common";
import {computed, getCurrentInstance, onMounted, provide, ref} from 'vue';
import {useRoute, useRouter} from 'vue-router';
import {getPage} from "@/api/business/page";
import { getNoPermissionButtons } from "@/api/system/menu";
import { formModeKey, vFormActiveElementKey, vFormSchemeKey } from "@/components/form/state.key";
import {genId} from "@/components/form/designer/util/common";
import useTagsViewStore from '@/store/modules/tagsView'

const pageInfo={
  scheme: null as Record<string, any> | null,
  formData: null as Record<string, any> | null
}
const scheme = ref<VFormScheme>([]);
const formData = ref<Record<string, any>>({})
const route = useRoute();
const router = useRouter();
const deviceType = getDeviceType()
provide(vFormSchemeKey, scheme)
const { proxy } = getCurrentInstance();
/**
 * 隐藏按钮
 * @param data
 * @param tempScheme
 */
function hiddenButtons(data, tempScheme) {
  recursionGetAllButtons(tempScheme,data);
  return tempScheme;
}


onMounted(() => {
  let pageId = route.query.pageId;
  getPage(pageId).then(async res => {
    let tempScheme = JSON.parse(res.data.pageScheme);
    //隐藏无权限按钮
    let parames = {"pageId": pageId, "menuId": route.query.menuId}
    let res1 = await getNoPermissionButtons(parames);
    if (res1.code == 200) {
      // //debugger;
      tempScheme = hiddenButtons(res1.data, tempScheme);
      scheme.value = tempScheme;
      formData.value ={};
    }
  })
});

/**
 * 递归获取所有button
 * @param schema
 * @param buttons
 */
function recursionGetAllButtons(schema:Object,buttons):any[] {
  //搜索所有下级：非布局，展示，按钮组件
  let i=0;
  let returnData=[];
  // //debugger;
  let childrens=[];
  let childReturnData=[];
  if(schema.children){
    childrens = schema.children;
  }
  //遍历所有子节点
  for(i=0;i<childrens.length;i++){
    if(childrens[i].category !=='layout' && childrens[i].category!=='display'){
      if(childrens[i].component=='ruoyi-ag-grid'){

       }else{
          //console.log("before call recursionGetAllButtons:{},{}",childrens[i],buttons)
          childReturnData = recursionGetAllButtons(childrens[i],buttons);

      }
      if(childReturnData.length>0){
        //console.log("after call childReturnData size:{},{}",childReturnData.length,childReturnData)
      }else {
        if (childrens[i].category == 'button') {
          //console.log("after call childReturnData childrens[i].category is 'button' ")
          if(buttons.includes(childrens[i].id)){
            childrens[i].attrs.show = false;
          }
        }else{
          //console.log("after call childReturnData childrens[i].category is not 'button' ")
        }
      }
    }else{
      //console.log("before2 call recursionGetAllButtons:{},{}",childrens[i],buttons)
      let childReturnData = recursionGetAllButtons(childrens[i],buttons);

      if(childReturnData.length>0){
        //console.log("after2 call childReturnData size:{},{}",childReturnData.length,childReturnData)
      }else{

        if (childrens[i].category == 'button') {
          //console.log("after2 call childReturnData childrens[i].category is 'button' ")
          if(buttons.includes(childrens[i].id)){
            childrens[i].attrs.show = false;
          }
        }else{
          //console.log("after2 call childReturnData childrens[i].category is not 'button' ")
        }
      }

    }
  }
  return returnData;
}
</script>

<style scoped>

</style>
